//
//  TodoListViewController.m
//  TodoDemo
//
//  Created by Tank on 16/2/21.
//  Copyright © 2016年 qtone. All rights reserved.
//

#import "TodoListViewController.h"
#import "TodoModel.h"
#import "AddViewController.h"
#import "DetailViewController.h"
#import "TodoDal.h"
#import <KVNProgress.h>

@interface TodoListViewController() <AddViewControllerDelegate, DetailViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *todoDatas;

@end

@implementation TodoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.todoDatas = [[TodoDal findAllTodos] mutableCopy];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return [self.todoDatas[1] count];
    }
    return [self.todoDatas[0] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return @"已完成";
    }
    return @"未完成";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProcessingCell"];
    if (indexPath.section == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"FinishedCell"];
    }
    TodoModel *model = self.todoDatas[indexPath.section][indexPath.row];
    cell.textLabel.text = model.content;
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *destVC = segue.destinationViewController;
    if ([destVC isKindOfClass:[AddViewController class]]) {
        AddViewController *addVC = (AddViewController *)destVC;
        addVC.delegate = self;
    } else if ([destVC isKindOfClass:[DetailViewController class]]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        TodoModel *model = self.todoDatas[indexPath.section][indexPath.row];
        DetailViewController *detailVC = (DetailViewController *)destVC;
        detailVC.delegate = self;
        detailVC.todoDetailModel = model;
        detailVC.indexPath = indexPath;
    }
}

#pragma mark - AddViewControllerDelegate
- (void)addTodoData:(NSString *)todoContent {
    [KVNProgress showWithStatus:@"正在添加..."];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 成功插入则刷新表格
        TodoModel *todoModel = [TodoDal insertTodoContent:todoContent];
        if (todoModel) {
            NSMutableArray *processingData = [self.todoDatas[0] mutableCopy];
            [processingData insertObject:todoModel atIndex:0];
            self.todoDatas[0] = processingData;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [KVNProgress showSuccess];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [KVNProgress showErrorWithStatus:@"添加失败!"];
            });
        }
    });
}

#pragma mark - DetailControllerDelegate
- (void)doneTodoData:(TodoModel *)todoModel indexPath:(NSIndexPath *)indexPath {
    [KVNProgress showWithStatus:@"正在更新..."];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 更改成功状态
        if ([TodoDal updateDone:todoModel]) {
            NSMutableArray *processingData = [self.todoDatas[0] mutableCopy];
            NSMutableArray *finishedData = [self.todoDatas[1] mutableCopy];
            [processingData removeObjectAtIndex:indexPath.row];
            [finishedData insertObject:todoModel atIndex:0];
            self.todoDatas[0] = processingData;
            self.todoDatas[1] = finishedData;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [KVNProgress showSuccess];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [KVNProgress showErrorWithStatus:@"更新失败!"];
            });
        }
    });
}

- (void)deleteTodoData:(TodoModel *)todoModel indexPath:(NSIndexPath *)indexPath {
    [KVNProgress showWithStatus:@"正在删除..."];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 更改删除状态
        if ([TodoDal remove:todoModel]) {
            NSMutableArray *data = [self.todoDatas[indexPath.section] mutableCopy];
            [data removeObjectAtIndex:indexPath.row];
            if (indexPath.section == 0) {
                self.todoDatas[0] = data;
            } else {
                self.todoDatas[1] = data;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [KVNProgress showSuccess];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [KVNProgress showErrorWithStatus:@"删除失败!"];
            });
        }
    });
}

@end
